const url = require("url");
const fs = require("fs");
const STATIC_SOURCE = ["png", "jpg", "jpeg", "json", "htm", "html", "xml", "js", "css", "less", "mp4", "mp3", "xlsx", "docs"];

let static = function static(path, res) {
    console.log(`.${path}`);
    fs.promises.readFile(`.${path}`).then(data => {
        res.write(data);
        res.end();
    }).catch(err => {
        console.log(err.message);
    });
};


exports._dealRequest = function _dealRequest(req, res, conn) {
    const path = url.parse(req.url, true);
    const pathname = path.pathname;
    const method = req.method;
    
    console.log(pathname);

    // 处理静态文件
    if(pathname.includes(".")) {
        let fix = pathname.substring(pathname.lastIndexOf(".") + 1, pathname.length);
        if(STATIC_SOURCE.includes(fix)) {
            // 直接返回静态资源
            static(pathname, res);
            return;
        }
    }

    // 处理跨域
    res.setHeader("Access-Control-Allow-Origin", '*');
    res.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization,token, content-type");
    res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    // 对跨域下的多发的 OPTIONS 请求进行处理
    if (req.method === "OPTIONS") { // CORS
        res.statusCode = 200;
        res.end();
    }

    // 处理费静态文件请求【数据请求】
    // 接收参数
    let obj = {
        path, // 解析的路径对象
        req, // http 的 request 对象
        res, // http 的 response 对象
        conn, // mysql 数据库连接对象
    };;
    if (method === "GET") { // GET 请求

        // 路由管控【请求控制器】
        switch (pathname) {
            // 聆心音乐首页 banner 图
            case '/api/lxMusic/common/static/banners':
                const { getHomeBannerData } = require("./pc/static");
                getHomeBannerData(res);
                break;
            // 聆心音乐首页推荐歌单
            case '/api/lxMusic/common/static/songsSheet':
                const { getHomeSongsSheetData } = require("./pc/static");
                getHomeSongsSheetData(res);
                break;
            case '/api/lxMusic/common/static/selectedActivity':
                const { getHomeSelectedActivityData } = require("./pc/static");
                getHomeSelectedActivityData(res);
                break;
            case '/api/lxMusic/common/static/topList':
                const { getHomeTopListData } = require("./pc/static");
                getHomeTopListData(res);
                break;
            case '/api/lxMusic/common/static/hotWord':
                const { getHomeHotWordData } = require("./pc/static");
                getHomeHotWordData(res);
                break;
            case '/api/lxMusic/common/static/searchWord':
                console.log("到后台了");
                // const path = url.parse(req.url, true);
                console.log(path);

                const { getHomeSEARCHWordData } = require("./pc/static");
                getHomeSEARCHWordData(res, path.query);
                break;
        }
    } else if (method === "POST") { // POST 请求

        // 获取参数
        let str = '';
        req.on('data', (thunk) => {
            str += thunk;
        });

        req.on('end', () => {
            console.log("^^^");
            
            // console.log(str);
        });

        // 路由管控【请求控制器】
        switch (pathname) {
            // 登录
            case '/api/lxMusic/common/user/login':
                const { login } = require("./common/user.js");
                login(obj);
                console.log('登录');
                break;
            // 注销
            case '/api/lxMusic/common/user/logout':
                const { logout } = require("./common/user.js");
                logout(obj);
                console.log('注销');
                break;
            // 注册
            case '/api/lxMusic/common/user/register':
                const { register } = require("./common/user.js");
                register(obj);

                console.log('注册');
                break;
            // 判断当前用户是否为聆心音乐的注册用户
            case '/api/lxMusic/common/user/isLxUser':
                const { isLxUser } = require("./common/user.js");
                isLxUser(obj);
                console.log('注册');
                break;
        }
    } else if (method === "PUT") { // PUT 请求

    } else if (method === "DELETE") { // DELETE 请求

    } else {
        console.warn("无效请求类型 ... ");
    }


};